package cn.tedu.zx.ams.adapter.serviceimpl;

import cn.tedu.zx.ams.adapter.mapper.PermissionMapper;
import cn.tedu.zx.ams.adapter.mapper.RolePermissionMapper;
import cn.tedu.zx.ams.service.PermissionService;
import cn.tedu.zx.common.exception.ServiceException;
import cn.tedu.zx.pojo.admin.entity.Permission;
import cn.tedu.zx.pojo.admin.entity.SuperAdminAuto;
import cn.tedu.zx.pojo.admin.parm.PermissionAddNewParam;
import cn.tedu.zx.pojo.admin.parm.PermissionUpdateParam;
import cn.tedu.zx.pojo.admin.vo.PermissionListItemVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

import static cn.tedu.zx.common.restful.ResponseCode.*;

@Service
@Slf4j
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public List<PermissionListItemVO> list() {
        log.debug("开始执行【查询权限列表】的业务");

        return permissionMapper.list();
    }

    @Override
    public void update(PermissionUpdateParam permissionUpdateParam) {
        log.debug("开始执行【修改权限数据】的业务");
        checkId(permissionUpdateParam.getId());
        checkName(permissionUpdateParam.getId(), permissionUpdateParam.getName());
        Permission permission = new Permission();
        BeanUtils.copyProperties(permissionUpdateParam, permission);
        permission.setUpdated(LocalDateTime.now());
        int updateCount = permissionMapper.updateById(permission);
        if (updateCount != 1) {
            String message = "权限修改失败";
            log.warn(message);
            throw new ServiceException(ERR_UPDATE, message);
        }
        log.debug("已完成【修改权限数据】的业务");
    }

    @Override
    public void delete(Long id) {
        log.debug("开始执行【删除权限数据】的业务");
        checkId(id);
        int deleteCount = permissionMapper.deleteById(id);
        if (deleteCount != 1) {
            String message = "权限删除失败";
            log.warn(message);
            throw new ServiceException(ERR_DELETE, message);
        }
        deleteRolePermissionBuPermissionId(id);
        log.debug("已完成【删除权限数据】的业务");
    }

    @Override
    public void addNew(PermissionAddNewParam permissionAddNewParam) {
        log.debug("开始执行【新增权限数据】的业务");
        checkName(null, permissionAddNewParam.getName());
        Permission permission = new Permission();
        BeanUtils.copyProperties(permissionAddNewParam, permission);
        permission.setCreated(LocalDateTime.now());
        permission.setUpdated(LocalDateTime.now());
        {
            int rows = permissionMapper.insert(permission);
            if (rows != 1) {

            }
        }
        Long id = permissionMapper.selectByValue(permission.getValue());
        //超级管理员自动增加新的权限
        SuperAdminAuto superAdminAuto = new SuperAdminAuto();
        superAdminAuto.setRoleId(1L);
        superAdminAuto.setPermissionId(id);
        superAdminAuto.setCreated(LocalDateTime.now());
        superAdminAuto.setUpdated(LocalDateTime.now());

        {
            int rows = rolePermissionMapper.insertSuperAdminAuto(superAdminAuto);
            if (rows != 1) {

            }
        }
        log.debug("已完成【新增权限数据】的业务");
    }

    private void checkId(Long id) {
        QueryWrapper<Permission> queryId = new QueryWrapper<>();
        queryId.eq("id", id);
        int countById = permissionMapper.selectCount(queryId);
        if (countById == 0) {
            String message = "权限id不存在";
            log.warn(message);
            throw new ServiceException(NOT_FOUND, message);
        }
    }

    private void checkName(Long id, String name) {
        QueryWrapper<Permission> queryName = new QueryWrapper<>();
        queryName.eq("name", name).ne("id", id);
        int countByName = permissionMapper.selectCount(queryName);
        if (countByName > 0) {
            String message = "权限名不存在";
            log.warn(message);
            throw new ServiceException(NOT_FOUND, message);
        }
    }

    private void deleteRolePermissionBuPermissionId(Long id) {
        int countRoleIdByPermissionId = rolePermissionMapper.countRoleIdByPermissionId(id);
        if (countRoleIdByPermissionId >= 1) {
            String message = "有" + countRoleIdByPermissionId + "个角色仅拥有该权限，请先删除或修改相关角色";
            log.warn(message);
            throw new ServiceException(ERR_DELETE, message);
        }
        int deleteCount = rolePermissionMapper.deleteByPermissionId(id);
        if (deleteCount < 1) {
            String message = "角色权限删除失败";
            log.warn(message);
            throw new ServiceException(ERR_DELETE, message);
        }
    }
}
